/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.domain;

import com.kcp.ko.fmtnmlib.types.Copyable;

import javax.persistence.*;
import java.util.Set;

/**
 * An Answer to a question.  This class also stores the user answer when
 * the user submits a Test for grading.
 * 
 * @author John Ericksen
 *
 */

@Entity
@Table( name="braintrain_answers" )
public class Answer extends BrainTrainBase implements Copyable<Answer>{
    
    private static final long serialVersionUID = -2835939013479228477L;
    
    private String answer;
    private boolean correct;
    private Integer answerOrder;
    
    //inverse mapping
    private Question questions;

    private Set<AnswerLog> answerLog;
    
    //transient
    private boolean userAnswer;
    
    public Answer copy() {
        Answer copy = new Answer();
        
        copy.setAnswer(this.getAnswer());
        copy.setCorrect(this.isCorrect());
        copy.setAnswerOrder(this.getAnswerOrder());
        copy.setVersion(this.getVersion());
        
        return copy;
    } 
    
    @Transient
    public String toString()
    {
        return "  " + getAnswer() + "\n";
    }
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="question_id", insertable=false, updatable=false)
    public Question getQuestions() {
        return questions;
    }
    public void setQuestions(Question questions) {
        this.questions = questions;
    }
    @Column(length=4000)
    public String getAnswer() {
        return answer;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    public boolean isCorrect() {
        return correct;
    }
    public void setCorrect(boolean correct) {
        this.correct = correct;
    }
    public Integer getAnswerOrder() {
        return answerOrder;
    }
    public void setAnswerOrder(Integer answerOrder) {
        this.answerOrder = answerOrder;
    }

    public void resetAnswer() {
        userAnswer = false;
    }

    @Transient
    public boolean isUserAnswer() {
        return userAnswer;
    }
    public void setUserAnswer(boolean userAnswer) {
        this.userAnswer = userAnswer;
    }

    @OneToMany(fetch=FetchType.LAZY)
    @JoinColumn(name="answer_id", insertable=false, updatable=false)
    public Set<AnswerLog> getAnswerLog() {
        return answerLog;
    }

    public void setAnswerLog(Set<AnswerLog> answerLog) {
        this.answerLog = answerLog;
    }
}
